@file:Suppress("unused")

package com.example.base.ext

import android.content.res.ColorStateList
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.InsetDrawable
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.VideoView
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.annotation.LayoutRes
import androidx.appcompat.widget.AppCompatImageView
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.coorchice.library.SuperTextView
import com.google.android.material.bottomnavigation.BottomNavigationItemView
import com.google.android.material.bottomnavigation.BottomNavigationMenuView
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.example.base.tools.CacheTool

/**
 * 给 ImageView 设置图片链接地址 并设置圆角尺寸
 * @param imgUrl 图片地址
 * @param placeholder 占位图
 * @param errorHolder 错误时占位图
 * @param corners 圆角值
 */
@BindingAdapter(value = ["imgUrl", "placeHolder", "errorHolder", "imgCorners"])
fun bindCornersImageUrl(
    view: AppCompatImageView,
    imgUrl: String,
    placeholder: Drawable,
    errorHolder: Drawable,
    corners: Int
) {
    Glide.with(view.context)
        .load(imgUrl)
        .disallowHardwareConfig()
        .placeholder(placeholder)
        .error(errorHolder)
        .apply(RequestOptions.bitmapTransform(RoundedCorners(corners)))
        .dontAnimate()
        .into(view)
}

/**
 * 给 ImageView 设置图片链接地址
 */
@BindingAdapter("imgUrl")
fun bindImageUrl(view: AppCompatImageView, imgUrl: String) {
    Glide.with(view.context)
        .load(imgUrl)
        .disallowHardwareConfig()
        .dontAnimate()
        .into(view)
}

/**  给 ImageView 设置 Vector 资源, 来源为 本地资源 id  **/
@BindingAdapter("vectorRes")
fun bindVectorDrawableForRes(view: ImageView, @DrawableRes resId: Int) {
    if (resId == 0) return
    view.setImageDrawable(VectorDrawableCompat.create(view.resources, resId, null))
}

/**  给 View 设置上外边距  **/
@BindingAdapter("android:layout_marginTop")
fun bindViewTopMargin(view: View, topMargin: Int) = view.setMarginTopPixel(topMargin)

/**  给 View 设置下外边距  **/
@BindingAdapter("android:layout_marginBottom")
fun bindViewBottomMargin(view: View, topMargin: Int) = view.setMarginBottomPixel(topMargin)

/**  给 View 设置左外边距  **/
@BindingAdapter("android:layout_marginStart")
fun bindViewStartMargin(view: View, topMargin: Int) = view.setMarginStartPixel(topMargin)

/**  给 View 设置右外边距  **/
@BindingAdapter("android:layout_marginEnd")
fun bindViewEndMargin(view: View, topMargin: Int) = view.setMarginEndPixel(topMargin)

/**  给 View 设置宽度  **/
@BindingAdapter("android:layout_width")
fun bindViewLayoutWidth(view: View, width: Float) = view.setLayoutWidth(width.toInt())

/**  给 View 设置高度  **/
@BindingAdapter("android:layout_height")
fun bindViewLayoutHeight(view: View, height: Float) = view.setLayoutHeight(height.toInt())

/**  给 SuperTextView 设置 solid 颜色  **/
@BindingAdapter("stv_solid")
fun bindSolidColor(view: SuperTextView, @ColorInt color: Int) {
    view.solid = color
}

/**  设置 recyclerView 是否固定高度  **/
@BindingAdapter("hasFixedSize")
fun bindRecyclerViewHasFixedSize(recyclerView: RecyclerView, hasFixedSize: Boolean) =
    recyclerView.setHasFixedSize(hasFixedSize)

/**
 * 设置 RecyclerView 的分割线样式
 * @param color 分割线颜色
 * @param dividerHeight 分割线高度
 * @param dividerLeftPadding 分割线左边距
 * @param dividerTopPadding 分割线上边距
 * @param dividerRightPadding 分割线右边距
 * @param dividerBottomPadding 分割线下边距
 */
@BindingAdapter(
    value = ["dividerColor", "dividerHeight", "dividerLeftPadding", "dividerTopPadding", "dividerRightPadding", "dividerBottomPadding"],
    requireAll = false
)
fun bindRecyclerViewDividerColor(
    recyclerView: RecyclerView, @ColorInt color: Int,
    dividerHeight: Int = 0,
    dividerLeftPadding: Int = 0,
    dividerTopPadding: Int = 0,
    dividerRightPadding: Int = 0,
    dividerBottomPadding: Int = 0
) {
    if ((color == 0) or (dividerHeight == 0)) return

    // prepare divider
    val divider = DividerItemDecoration(recyclerView.context, DividerItemDecoration.VERTICAL)

    // init drawable
    val gradientDrawable = GradientDrawable()
    gradientDrawable.color = ColorStateList.valueOf(color)
    gradientDrawable.setSize(-1, dividerHeight)
    val insetDrawable = InsetDrawable(
        gradientDrawable,
        dividerLeftPadding,
        dividerTopPadding,
        dividerRightPadding,
        dividerBottomPadding
    )

    // set divider to RecyclerView
    divider.setDrawable(insetDrawable)
    recyclerView.addItemDecoration(divider)
}

/**  设置 ViewPager 的 Adapter  **/
@BindingAdapter("pagerAdapter")
fun bindViewPagerAdapter(viewPager: ViewPager, pagerAdapter: PagerAdapter) {
    viewPager.adapter = pagerAdapter
}

/**  设置 ViewPager 当前选中的 Item 索引  **/
@BindingAdapter("currentItemIndex")
fun bindViewPagerCurrentItem(viewPager: ViewPager, currentItemIndex: Int) {
    if (currentItemIndex == 0) return
    viewPager.currentItem = currentItemIndex
}

/**  设置 VideoView 的视频路径, 设置后默认是读取缓存的文件地址  **/
@BindingAdapter("videoPath")
fun bindVideoViewVideoPath(videoView: VideoView, videoPath: String) =
    videoView.setVideoPath(CacheTool.getProxyUrl(videoPath))

/**  设置 BottomNavigationView 的角标布局  **/
@BindingAdapter("badgeView")
fun bindBottomNavigationViewBadgeView(bottomNavigationView: BottomNavigationView, @LayoutRes badgeViewLayoutId: Int) {
    val menuView = bottomNavigationView.getChildAt(0) as BottomNavigationMenuView

    repeat(menuView.childCount) { index ->
        val badgeView = LayoutInflater.from(bottomNavigationView.context).inflate(badgeViewLayoutId, menuView, false)
        val itemView = menuView.getChildAt(index) as BottomNavigationItemView
        itemView.addView(badgeView)
    }
}